00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef BRUSH_OCTREE_HPP
00037 #define BRUSH_OCTREE_HPP
00038
00039 #include "deGlobalTypes.hpp"
00040 #include "deList.hpp"
00041 #include "deArray.hpp"
00042 #include "deBrush_priv.hpp"
00043
00044 #include "deHash.hpp"
00045 #include "deFunctors.hpp"
00046
00047 class deBrushOctree
00048 {
00049 public:
00050 class deIndexedPoly
00051 {
00052 public:
00053
00054 s32 NumVerts;
00055 long PolyIndex;
00056 long TextureIndex;
00057 u16 * PosIndices;
00058 u16 * NormIndices;
00059 u16 * MatIndices;
00060 deDouble Dist;
00061 deVec3d Normal;
00062 ~deIndexedPoly()
00063 { delete[] PosIndices; }
00064 deIndexedPoly(s32 vertnum = 3)
00065 {
00066
00067 NumVerts = vertnum;
00068 PosIndices = new u16[NumVerts*3];
00069 NormIndices= &PosIndices[NumVerts];
00070 MatIndices = &PosIndices[NumVerts*2];
00071 TextureIndex = -1;
00072 PolyIndex = -1;
00073 }
00074 deIndexedPoly(const deIndexedPoly & ref)
00075 {
00076
00077 Normal = ref.Normal;
00078 Dist = ref.Dist;
00079 NumVerts = ref.NumVerts;
00080 TextureIndex = ref.TextureIndex;
00081 PosIndices = new u16[NumVerts*3];
00082 NormIndices= &PosIndices[NumVerts];
00083 MatIndices = &PosIndices[NumVerts*2];
00084 memcpy(PosIndices, ref.PosIndices, NumVerts*sizeof(u16)*3);
00085 PolyIndex = -1;
00086 }
00087 deIndexedPoly & operator=(const deIndexedPoly & ref)
00088 {
00089 Normal = ref.Normal;
00090 Dist = ref.Dist;
00091 TextureIndex = ref.TextureIndex;
00092 NumVerts = ref.NumVerts;
00093 if (NumVerts != ref.NumVerts)
00094 {
00095 delete[] PosIndices;
00096 PosIndices = new u16[NumVerts*3];
00097 NormIndices= &PosIndices[NumVerts];
00098 MatIndices = &PosIndices[NumVerts*2];
00099 }
00100 memcpy(PosIndices, ref.PosIndices, NumVerts*sizeof(u16)*3);
00101 PolyIndex = -1;
00102 return *this;
00103 }
00104 void Resize(s32 vertnum)
00105 {
00106 if (vertnum == NumVerts) return;
00107 u16* pTemp;
00108 NumVerts = vertnum;
00109
00110 pTemp = new u16[NumVerts*3];
00111 memcpy(pTemp, PosIndices, NumVerts*sizeof(u16)*3);
00112 delete[] PosIndices;
00113 PosIndices = pTemp;
00114 NormIndices= &PosIndices[NumVerts];
00115 MatIndices = &PosIndices[NumVerts*2];
00116 }
00117 };
00118
00119 struct deVertMat
00120 {
00121 deARGB Diffuse;
00122 deTexCoord UV;
00123 };
00124
00125 private:
00126 struct OctNode
00127 {
00128 long Depth;
00129 DWORD Contents;
00130 deVec3d Min, Max;
00131 struct OctNode* Children[9];
00132 deTArray <long> PolyIndexList;
00133 };
00134
00135 public:
00136 deBrushOctree();
00137 ~deBrushOctree();
00138
00139 deBoolean AddPositions(deVec3d *VertArray, u16 NumVerts, u16 &Offset);
00140 deBoolean AddNormals(deVec3d *VertArray, u16 NumVerts, u16 &Offset);
00141 deBoolean AddVertMats(deVertMat *VertArray, u16 NumVerts, u16 &Offset);
00142 deBoolean AddPolyGetRef(deIndexedPoly* & poly);
00143 deBoolean AddPoly(deIndexedPoly * poly, u16 PosOffset, u16 NormOffset, u16 MatOffset);
00144
00145 deBoolean GetPoly(deIndexedPoly* & poly, long PolyIndex);
00146
00147 deBoolean RemovePoly(const deIndexedPoly * poly);
00148
00149 deBoolean Generate();
00150 deBoolean Clear();
00151
00152 deBoolean GetPolysInBox(const deVec3d & Min, const deVec3d & Max, deTArray <deIndexedPoly*> &PolyArray, OctNode * node = NULL);
00153 deBoolean GetAllPolys(deTArray <deIndexedPoly*>* &PolyArray);
00154 deBoolean GetPolyInfo(deIndexedPoly* poly, deVec3d* &PosArray, deVec3d* &NormArray, deVertMat* &VertMatArray);
00155
00156
00157
00158
00159
00160
00161
00162
00163 private:
00164 OctNode *m_Root;
00165 deTList <long> m_PolyAddList;
00166 deTList <long> m_PolyRemoveList;
00167 deTArray <deIndexedPoly*> m_PolyArray;
00168 deTArray <deTArray<OctNode*> > m_PolyNodes;
00169 deTArray <deVec3d> m_Positions;
00170 deTArray <deVec3d> m_Normals;
00171 deTArray <deVertMat> m_VertMats;
00172
00173 dePlane m_SplittingPlane;
00174 deTHashFunctor<u16,HashEdge> m_SplitPosHash;
00175 deTHashFunctor<u16,HashEdge> m_SplitNormHash;
00176 deTHashFunctor<u16,HashEdge> m_SplitMatHash;
00177
00178
00179 void DestroyNode(OctNode * node);
00180
00181 deBoolean InsertPolyIntoTree(long PolyIndex);
00182 u8 DecideChildNodes(long PolyIndex, OctNode* Node);
00183 OctNode* GetSmallestNodeForAABB(const deAABB & bbox);
00184 deBoolean InsertPolyIntoNode(long PolyIndex, OctNode* Node);
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 static char FrontmostOctNodeCorner(const deVec3d & norm);
00199 static void OctNodeCorner(const OctNode* node, const char Corner, deVec3d & vec);
00200 static void OctNodeMidPoint(const OctNode* node, const char MidPoint, deVec3d & vec);
00201 static deBoolean OctNodeIntersectsPlane(OctNode *node, const dePlane & plane);
00202 static inline void OctNodeCenter(const OctNode* node, deVec3d & vec)
00203 {
00204 vec = node->Min; vec += node->Max; vec *= 0.5;
00205 }
00206
00207
00208
00209 char TriLineIntersect( const deIndexedPoly * T, u16 VertNum,
00210 const deVec3d & LinePt1, const deVec3d & LinePt2,
00211 deVec3d & Res, deBoolean & HitEdge);
00212 deBoolean PolyPlaneIntersect(const deVec3d & norm, const deDouble & dist, const deIndexedPoly * poly, deVec3d* & ResPair);
00213 deBoolean PolyPolyIntersect(const deIndexedPoly * p1, const deIndexedPoly * p2);
00214 deBoolean MakePlane(long PolyIndex);
00215 deBoolean MakePlane(deIndexedPoly * input);
00216 deBoolean SplitPoly(const deIndexedPoly * input, deIndexedPoly* & out1, deIndexedPoly* & out2);
00217 u16 SplitEdgePos(const HashEdge & Edge, deDouble & Percent);
00218 u16 SplitEdgeNorm(const HashEdge & Edge, const deDouble & Percent);
00219 u16 SplitEdgeMat(const HashEdge & Edge, const deDouble & Percent);
00220
00221 deBoolean SplitAllPolys(deIndexedPoly* splitter);
00222
00223 deBoolean AssembleVertexBuffer(IdeVertexBuffer * pVB);
00224 deBoolean AssembleVertexBufferNoNormals(IdeVertexBuffer * pVB);
00225 deBoolean TriangulateIndices(const deIndexedPoly * Poly, u16 LastVertIndex, u16 * IndexArray3);
00226
00227 void TEST();
00228 };
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 #endif